﻿using DataAccess;
using DataAccess.DAL;
using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Agent.Controllers
{
    public class AgentBaseController : Controller
    {

#if DEBUG
        //获取当前域名
        string domain = "18212340001.f-9.cc";
        //   string url = "zsxs.sifuku.cn";
#endif
#if !DEBUG
        string domain = XYD.Common.Request.GetHost();
#endif

        private SysConfig sysConfig;

        public SysConfig SysConfig
        {
            get
            {

                if (sysConfig == null)
                {
                    // sysConfig = new DataAccess.DAL.SysConfigDAL().GetByDomin(domain);

                    if (!RedisHelper.Exist(RedisKey.SysConfig(domain)))
                    {

                        sysConfig = new DataAccess.DAL.SysConfigDAL().GetByDomin(domain);
                        if (sysConfig == null)
                            sysConfig = new DataAccess.Models.SysConfig();
                        RedisHelper.Single_Set_Itme<SysConfig>(RedisKey.SysConfig(domain), sysConfig);
                    }
                    return RedisHelper.Single_Get_Itme<SysConfig>(RedisKey.SysConfig(domain));

                }
                return sysConfig;

            }

        }

       
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.RouteData.Values != null && filterContext.RouteData.Values.ContainsKey("agent"))
            {
                object resultValue = null;
                if (filterContext.RouteData.Values.TryGetValue("agent", out resultValue) && resultValue != null)
                    agentUserName = resultValue.ToString();
            }

            ViewBag.aid = agentUserName;
            base.OnActionExecuting(filterContext);
        }

        private string agentUserName;
        /// <summary>
        /// 代理用户名称
        /// </summary>
        public string AgentUserName
        {
            get
            {
                return agentUserName;
            }
            set
            {
                agentUserName = AgentUserName;
            }
        }


        private string site_pid;

        public string Site_Pid
        {
            get
            {
                if (string.IsNullOrEmpty(site_pid))
                {
                    //通过域名和 代理账号获取网站pid
                    // AgencyUsersDAL dal = new AgencyUsersDAL();
                    //site_pid=  dal.GetSitePid(agentUserName,SysConfig.promoter_id.ToString());

                    //if (string.IsNullOrEmpty(site_pid))
                    //{
                    //    site_pid = SysConfig.def_pid;
                    //}
                    if (SysConfig == null || SysConfig.promoter_id == 0)
                        {
                            site_pid = System.Configuration.ConfigurationManager.AppSettings["def_pid"];
                        }
                    else if (!RedisHelper.Exist(RedisKey.Site_Pid(agentUserName, SysConfig.promoter_id.ToString())))
                    {
                         
                            AgencyUsersDAL dal = new AgencyUsersDAL();
                            site_pid = dal.GetSitePid(agentUserName, SysConfig.promoter_id.ToString());

                            //如果代理pid为空 取推广者默认pid
                            if (string.IsNullOrEmpty(site_pid))
                            {
                                //如果为空用默认的pid
                                site_pid = string.IsNullOrEmpty(SysConfig.def_pid) ? System.Configuration.ConfigurationManager.AppSettings["def_pid"] : SysConfig.def_pid;
                            }
                           
                                RedisHelper.Single_Set_Itme<string>(RedisKey.Site_Pid(agentUserName, SysConfig.promoter_id.ToString()), site_pid);
                           
                            
                    }
                    return RedisHelper.Single_Get_Itme<string>(RedisKey.Site_Pid(agentUserName, SysConfig.promoter_id.ToString()));


                }

                return site_pid;
            }



        }
    }
}